package leetcode;

import java.util.ArrayList;
import java.util.List;

import Tree.CreateByPreAndIn;
import Tree.TreeNode;

public class BinaryTreePaths {

	public static void main(String[] args) {
		int[] pre = {2, 3, 9, 6, 8, 7, 5};
		int[] in = {3, 9, 2, 7, 8, 5, 6};
		TreeNode root = CreateByPreAndIn.reConstructBinaryTree(pre, in);
		System.out.println(root.val);
		BinaryTreePaths object = new BinaryTreePaths();
		List<String> resList = object.binaryTreePaths(root);
		System.out.println(resList);
	}
	
	public List<String> binaryTreePaths(TreeNode root) {
		List<String> res = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		help(root, res, sb);
		System.out.println(res);
		return res;
	}
	
	public void help(TreeNode node, List<String> res, StringBuilder sb){
		if(node == null){
			return;
		}
		if(node.left == null && node.right == null){
			sb.append(node.val);
			res.add(sb.toString());
			return;
		}
		sb.append(node.val).append("->");
		System.out.println("sb: " + sb.toString());
		StringBuilder temp = new StringBuilder(sb);
		if(node.left != null){
			help(node.left, res, temp);
		}
		if(node.right != null){
			help(node.right, res, sb);
		}
	}
}
